package com.spring.transaction.demotransaction.service;

import com.spring.transaction.demotransaction.dao.UserDao;
import com.spring.transaction.demotransaction.dao.UserExtDao;
import com.spring.transaction.demotransaction.entity.User;
import com.spring.transaction.demotransaction.entity.UserExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserExtDao userExtDao;

    @Override
    public User insertUser(User user, boolean throwException) {
        userDao.insert(user);
        if (throwException) {
            System.out.println("保存用户数据模拟异常");
            throw new RuntimeException("保存用户数据模拟异常");
        }
        return user;
    }

    @Override
    public void insertUserExt(UserExt userExt, boolean throwException) {
        userExtDao.insert(userExt);
        if (throwException) {
            System.out.println("保存用户扩展信息模拟异常");
            throw new RuntimeException("保存用户扩展信息模拟异常");
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public User insertUserWithRequired(User user, boolean throwException) {
        userDao.insert(user);
        if (throwException) {
            System.out.println("保存用户数据模拟异常");
            throw new RuntimeException("保存用户数据模拟异常");
        }
        return user;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insertUserExtWithRequired(UserExt userExt, boolean throwException) {
        userExtDao.insert(userExt);
        if (throwException) {
            System.out.println("保存用户扩展信息模拟异常");
            throw new RuntimeException("保存用户扩展信息模拟异常");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public User insertUserWithRequiredNew(User user, boolean throwException) {
        userDao.insert(user);
        if (throwException) {
            System.out.println("保存用户数据模拟异常");
            throw new RuntimeException("保存用户数据模拟异常");
        }
        return user;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void insertUserExtWithRequiredNew(UserExt userExt, boolean throwException) {
        userExtDao.insert(userExt);
        if (throwException) {
            System.out.println("保存用户扩展信息模拟异常");
            throw new RuntimeException("保存用户扩展信息模拟异常");
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NESTED)
    public User insertUserWithNested(User user, boolean throwException) {
        userDao.insert(user);
        if (throwException) {
            System.out.println("保存用户数据模拟异常");
            throw new RuntimeException("保存用户数据模拟异常");
        }
        return user;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NESTED)
    public void insertUserExtWithNested(UserExt userExt, boolean throwException) {
        userExtDao.insert(userExt);
        if (throwException) {
            System.out.println("保存用户扩展信息模拟异常");
            throw new RuntimeException("保存用户扩展信息模拟异常");
        }
    }
}
